<template>
    <div class='readCardjy'>
      <el-button type="primary" @click="btnClicks">读取</el-button>
     
  </div>
</template>
<script>
export default {
  props:['id'],
  name:'read-cardjy',
  data(){
      return{
          webSocket:null,
          src:"",
          timer:null
      }
  },
  created(){
     
  },
  methods:{
      btnClicks(){
         
          this.openStock();
      },
      openStock(){
        //华旭
              let sef=this;
              var wsObj = new WebSocket("ws://127.0.0.1:17171");//建立连接

              //连接成功后的回调函数
              wsObj.onopen = function(ev)
              {  
                  console.log(ev);
                  wsObj.send("readcard");//发送读卡请求
              };

              //返回消息后的回调函数
              wsObj.onmessage = function(ev)//获取后端响应
              {  
                  console.log(ev.data)
                  let data=ev.data;
                  data=JSON.parse(data)
                  console.log(data)
                  let begin_data="data:image/jpeg;base64,";
                  let datas={};
                  let BirthDate=data.Birth;
                 
                  datas.Born=BirthDate.substring(0,4)+"-"+BirthDate.substring(4,6)+"-"+BirthDate.substring(6);
                  datas.labelA=data.Name;
                  datas.Sex=data.Sex;
                  datas.mz=data.Ethnicity+"族";
                  datas.Address=data.Address;
                  datas.CardNo=data.ID;
                  datas.image=begin_data+data.photo;
                  var date = new Date();
                  let curYear=date .getFullYear(); //获取完整的年份(4位)
                  let age=Number(curYear)-Number(BirthDate.substring(0,4));
                  age=age.toFixed(0);
                  datas.age=age;
                  
                  let ids=sef.id.split("|");
                  for(let i=0;i<ids.length;i++){
                      let id_=ids[i].split(":");
                      if(id_[0]!="image"){
                          let data={};
                          let val=datas[id_[0]];
                          data.id=id_[1];
                          data.val=val;
                          
                          sef.$emit("input",JSON.stringify(data));
                     }else{
                        sef.$post("business","/api/file/fileUploadBase64",{data:data.photo,model:"头像",type:"头像",name:"头像"}).then(data=>{
                        
                            let url=data[0].url;
                            console.log("uploadImg==========================");
                            console.log(url);
                            let data_={};
                            data_.id=id_[1];
                            data_.val=url;
                            sef.$emit("input",JSON.stringify(data_));
                        });
                    }
                      
                  }


                  wsObj.close();
              };

              //出现错误后的回调函数
              wsObj.onerror = function(ev)
              {
                  console.log(ev);
              };

              //连接关闭后的回调函数
              wsObj.onclose = function(ev)
              {
                  console.log("Connection closed.");      
              };
          
      }
  }
}
</script>